package com.example.zoo;

import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

public class Zoo extends Activity {
	
	private Zoo me;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zoo);
		
		Bundle extra = this.getIntent().getExtras();
		if(extra != null){
			if(extra.getString("back") != null){
			}else AppManager.getAppManager().addActivity(this);
		}else AppManager.getAppManager().addActivity(this);

		//Log.v("appstack", AppManager.getAppManager().getSize()+"");
		CheckNetworkState();
		me = this;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.zoo, menu);
		return true;
	}

	public void toMap(View vue) {
		startActivity(new Intent(this, MapChoice.class));
	}

	public void toList(View vue) {
		startActivity(new Intent(this, List.class));
	}

	public void toQuestion(View vue) {
		startActivity(new Intent(this, QuestionActivity.class));
	}

	public void toMore(View vue) {
		WindowManager manage = getWindowManager();
		Display display = manage.getDefaultDisplay();
		int screenwidth = display.getWidth();
		int screenheight = display.getHeight();
		ViewGroup myView = (ViewGroup) getLayoutInflater().inflate(
				R.layout.popmenu, null);
		PopupWindow window = new PopupWindow(myView, 480, 50);
		window.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.welcome));
		window.setAnimationStyle(R.style.AnimationFade);
		window.setOutsideTouchable(true);
		window.setFocusable(true);
		window.setTouchable(true);
		window.showAsDropDown(myView, 250, screenheight - 130);
		Button about = (Button) window.getContentView().findViewById(R.id.about);
		Button quit = (Button) window.getContentView().findViewById(R.id.quit);
		about.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(me)
				.setTitle("Qui sommes-nous")
				.setMessage("Created by : \nLV Yuchang & LIU Qiang\nMaster 2 E-Services\nUniversity Lille 1")
				.setPositiveButton("OK",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								
							}
						}).show();
			}

		});

		quit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AppManager.getAppManager().AppExit(me.getApplicationContext());
			}
		});
	}

	public void toShare(View vue) {
		Uri shareonfb = Uri
				.parse("http://www.facebook.com/sharer/sharer.php?u=http://www.mairie-lille.fr/cms/accueil/sport-loisirs/zoo-lille&t=Parc zoologique de Lille");
		Intent intent = new Intent(Intent.ACTION_VIEW, shareonfb);
		startActivity(intent);
	}
	
	public void toBack(View vue){
		//Log.v("appstack1", AppManager.getAppManager().getSize()+"");
		Activity previousActivity = AppManager.getAppManager().previousActivity();
		Intent intent = new Intent(this, previousActivity.getClass());
		intent.putExtra("back", "back");
		AppManager.getAppManager().finishActivity();
		//Log.v("appstack2", AppManager.getAppManager().getSize()+"");
		startActivity(intent);
	}
	
	// Check network
	public boolean CheckNetworkState() {
		ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		State mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
				.getState();
		State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();
		if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
			Log.v("Connectivity", "2G or 3G is actived !!!");
			return true;
		}
		if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
			Log.v("Connectivity", "Wifi is actived !!!");
			return true;
		}
		showTips();
		return false;
	}

	private void showTips() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(android.R.drawable.ic_dialog_alert);
		builder.setTitle("Status of connectivity");
		builder.setMessage("Would you active the connectivity(Wifi)?");
		builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
			}
		});
		builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		builder.create();
		builder.show();
	}

}
